<?php

class UserrankController extends AppController {

	public $name = 'userrank';
	
	function beforeFilter() {
	    parent::beforeFilter(); 
		$this->C_auth->autoRedirect = false; //For override login
	    $this->C_auth->allowedActions = array('index','add','edit','delrank');
	}
	
	public function index() {
		$query = array(
			'order' => ' id desc ', 
			'fields' => array('userrank.*'), 
		);
		$result = POP::exe('[@@:UM userrank][@@:QU find:all]',$query);		
		$query = array(
			'order' => ' id asc ', 
			'fields' => array('privilege.*'), 
		);
		$array = POP::exe('[@@:UM privilege][@@:QU find:all]',$query);
        $privilege_list = array();		
	    foreach($array as $v) {
		    $privilege_list[$v['privilege']['id']] = $v['privilege'];
	    }
        foreach($result as $key=>$value) {
	        $privilege = '';
	        if(!isset($result[$key]['userrank']['pri'])) {
		        $result[$key]['userrank']['pri'] = '';
	        }
	        if($value['userrank']['privilege']) {
		        $privilege = unserialize($value['userrank']['privilege']);
		        if($privilege) {
			        foreach($privilege as $k=>$v) {
				        $result[$key]['userrank']['pri'] .= $privilege_list[$k]['privilege_name'].':'.$v.' ';
			        }
		        }
	        }
        }
		$this->set('result',$result);		
	}		
	
	public function edit($id) {	
	    if(!empty($this->data)) {
		    $rank_id = intval($this->data['id']);
            $array = array();
            $privilege = Request::get_args('privilege');
            $privilege_name = Request::get_args('privilege_name');
            if($privilege) {
	            foreach($privilege as $key=>$value) {
		            $array[$key] = $value;
		            if(isset($privilege_name[$key]) && $privilege_name[$key]) {
			            $array[$key] = $privilege_name[$key];
		            }
	            }
            }
            $this->data['privilege'] = serialize($array);
			unset($this->data['id']);			
            $safedata['userrank'] = $this->data;
			$query['keyid'] = $rank_id;
			$query['set'] = $safedata;			
			if (POP::exe('[@@:UM userrank][@@:QU save]',$query)) {			
                $this->flash('修改成功！', Router::url('admin/userrank/index'));
            }else {			
		        $this->flash('修改失败！', Router::url('admin/userrank/edit/'.$rank_id));
		    }		
		}else {
			$rank_id = intval($id);
			$query = array(
				'conditions' => array('userrank.id' => $rank_id), 
				'fields' => array('userrank.*'), 
			);
			$info = POP::exe('[@@:UM userrank][@@:QU find:first]',$query);
			$info = $info['userrank'];				
			$query = array(
				'order' => ' id asc ', 
				'fields' => array('privilege.*'), 
			);
			$array = POP::exe('[@@:UM privilege][@@:QU find:all]',$query);
			$privilege_list = array();	
			foreach($array as $v) {
				$privilege_list[$v['privilege']['id']] = $v['privilege'];
			}
			$privilege = unserialize($info['privilege']);
			$this->set('info',$info);       
			$this->set('privilege_list',$privilege_list);
			$this->set('privilege',$privilege);	
        }		
    }	
	
	public function add() {
	    if(!empty($this->data)) {
            $array = array();
            $privilege = Request::get_args('privilege');
            $privilege_name = Request::get_args('privilege_name');
            if($privilege) {
	            foreach($privilege as $key=>$value) {
		            $array[$key] = $value;
		            if(isset($privilege_name[$key]) && $privilege_name[$key]) {
			            $array[$key] = $privilege_name[$key];
		            }
	            }
            }
            $this->data['privilege'] = serialize($array);
            $safedata['userrank'] = $this->data;
			if (POP::exe('[@@:UM userrank][@@:QU save]',$safedata)) {	
                $this->flash('添加成功！', Router::url('admin/userrank/index'));
            }else {			
		        $this->flash('添加失败！', Router::url('admin/userrank/add'));
		    }

		} else {
			$query = array(
				'order' => ' id asc ', 
				'fields' => array('privilege.*'), 
			);
			$array = POP::exe('[@@:UM privilege][@@:QU find:all]',$query);		
            $privilege_list = array();
	        foreach($array as $v) {
		        $privilege_list[$v['privilege']['id']] = $v['privilege'];
	        }
		    $this->set('privilege_list',$privilege_list);
        }		
	}	

	public function delrank() {	
        /* get */
        $rankid=Request::get_args('rank_id');
        if($rankid){
	        $id=implode(",", $rankid);
        }else{
	        $id = intval(Request::get_args('id'));
        }
        if(!$id) {
	        $this->flash('非法操作！', Router::url('admin/userrank/index'));
        } else {
			$query  = array();		
			$query['keyid'] = true;
			$safeData['user'] = array('rank_id' => 1);				
			$query['set'] = $safeData;
		    $query['conditions'] = array('rank_id' => $id);			
			$query1 = array('conditions'=>array('userrank.id' => $id));			
			if(POP::exe('[@@:UM userrank][@@:QU del]',$query1) and POP::exe('[@@:UM user][@@:QU save]',$query)) {		
                $this->flash('操作成功！', Router::url('admin/userrank/index'));
            } else {
                $this->flash('操作失败！', Router::url('admin/userrank/index'));
            }
        }		
	}

}
?> 